/*
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.examples.javaeditor;

import org.eclipse.jface.action.*;
import org.eclipse.ui.*;
import org.eclipse.ui.editors.text.TextEditorActionContributor;
import org.eclipse.ui.texteditor.*;

/**
 * Contributes interesting Java actions to the desktop's Edit menu and the toolbar.
 */
public class JavaActionContributor extends TextEditorActionContributor {

    protected RetargetTextEditorAction fContentAssistProposal;
    protected RetargetTextEditorAction fContentAssistTip;
    protected TextEditorAction fTogglePresentation;

    /**
     * Default constructor.
     */
    public JavaActionContributor() {
        super();
        fContentAssistProposal = new RetargetTextEditorAction(JavaEditorMessages
                .getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
        fContentAssistProposal
                .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
        fContentAssistTip = new RetargetTextEditorAction(JavaEditorMessages.getResourceBundle(),
                "ContentAssistTip."); //$NON-NLS-1$
        fContentAssistTip
                .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
        fTogglePresentation = new PresentationAction();
    }

    /*
     * @see IEditorActionBarContributor#init(IActionBars)
     */

    public void init( IActionBars bars ) {
        super.init(bars);

        IMenuManager menuManager = bars.getMenuManager();
        IMenuManager editMenu = menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
        if (editMenu != null) {
            editMenu.add(new Separator());
            editMenu.add(fContentAssistProposal);
            editMenu.add(fContentAssistTip);
        }

        IToolBarManager toolBarManager = bars.getToolBarManager();
        if (toolBarManager != null) {
            toolBarManager.add(new Separator());
            toolBarManager.add(fTogglePresentation);
        }
    }

    private void doSetActiveEditor( IEditorPart part ) {
        super.setActiveEditor(part);

        ITextEditor editor = null;
        if (part instanceof ITextEditor)
            editor = (ITextEditor) part;

        fContentAssistProposal.setAction(getAction(editor, "ContentAssistProposal")); //$NON-NLS-1$
        fContentAssistTip.setAction(getAction(editor, "ContentAssistTip")); //$NON-NLS-1$
        
        

        fTogglePresentation.setEditor(editor);
        fTogglePresentation.update();
    }

    /*
     * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
     */

    public void setActiveEditor( IEditorPart part ) {
        super.setActiveEditor(part);
        doSetActiveEditor(part);
    }

    /*
     * @see IEditorActionBarContributor#dispose()
     */

    public void dispose() {
        doSetActiveEditor(null);
        super.dispose();
    }
}
